package com.ryan.smile.util;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedHashMap;

import android.content.Context;
import android.content.res.AssetFileDescriptor;

import com.ryan.smile.model.TitleModel;
import com.ryan.smile.model.ViewModel;

public class FileUtil {

    private static LinkedHashMap<Integer, ArrayList<TitleModel>> idxMap = null;
    private static ArrayList<Integer> ids = null;
    public static LinkedHashMap<Integer, ArrayList<TitleModel>> getIdxMap(Context ctx){
        if(idxMap==null)
            init(ctx);
        return idxMap;
    }
    private static void init(Context ctx) {
        LinkedHashMap<Integer, ArrayList<TitleModel>> rst = new LinkedHashMap<Integer, ArrayList<TitleModel>>();
        ArrayList<Integer> idRst = new ArrayList<Integer>();
        try {
            AssetFileDescriptor afd = ctx.getAssets().openFd("idx"+Constant.FILE_SUFFIX);
            FileInputStream fis = afd.createInputStream();
            BufferedReader br  = new BufferedReader(new InputStreamReader(fis));
            String red = null;
            while( (red=br.readLine())!=null ){
                String[] split = red.split(Constant.FILE_SPLIT);
                int idx = Integer.parseInt(split[0]);
                String body = split[1];
                String[] idxArray = body.split(",");
                ArrayList<TitleModel> seekList = new ArrayList<TitleModel>();
                for (int i=0;i<idxArray.length;i++) {
                    TitleModel tm = new TitleModel();
                    tm.setSeek(Long.parseLong(idxArray[i]));
                    if(i>0){
                        tm.setLastSeek(Long.parseLong(idxArray[i-1]));
                    }
                    if(i<idxArray.length-1){
                        tm.setNextSeek(Long.parseLong(idxArray[i+1]));
                    }
                    seekList.add(tm);
                }
                idRst.add(idx);
                rst.put(idx, seekList);
            }
            afd.close();
        } catch (Exception e) {
            LogUtil.e("E", "e",e);
        }
        idxMap = rst;    
        ids = idRst;
    }
    public static ArrayList<Integer> getIds(Context ctx){
        if(ids==null)
            init(ctx);
        return ids;
    }
    
    public static ArrayList<TitleModel> getTitle(Context ctx,int stage){
        ArrayList<TitleModel> rst = new ArrayList<TitleModel>();
        try {
            AssetFileDescriptor afd = ctx.getAssets().openFd("f/"+stage+Constant.FILE_SUFFIX);
            FileInputStream fis = afd.createInputStream();
            BufferedReader br  = new BufferedReader(new InputStreamReader(fis,"gbk"));
            String red = null;
            int seek=0;
            TitleModel tm1 = null;
            TitleModel tm2 = null;
            while( (red=br.readLine())!=null ){
                if(tm1==null){
                    tm1 = new TitleModel();
                    tm1.setSeek(seek);
                    tm1.setDesc(red.length()<Constant.TITLE_LEN?red:red.substring(0,Constant.TITLE_LEN));
                }else{
                    tm2 = new TitleModel();
                    tm2.setSeek(seek);
                    String desc =red.length()<Constant.TITLE_LEN?red:red.substring(0,Constant.TITLE_LEN);
                    tm2.setDesc(desc.replace("\\n", ""));
                    tm1.setNextSeek(seek);
                    tm2.setLastSeek(tm1.getSeek());
                    rst.add(tm1);
                    tm1 = tm2;
                    tm2 = null;
                }
                seek+=red.getBytes().length+2;//0d 0a
            }
            if(tm1!=null){
                rst.add(tm1);
            }
            afd.close();
        } catch (Exception e) {
            LogUtil.e("E", "e",e);
        }
        return rst;
    }
    
    public static ViewModel getView(Context ctx,int stage,long seek){
        try {
            AssetFileDescriptor afd = ctx.getAssets().openFd("f/"+stage+Constant.FILE_SUFFIX);
            FileInputStream fis = afd.createInputStream();
            BufferedReader br  = new BufferedReader(new InputStreamReader(fis,"gbk"));
            br.skip(seek);
            String red = null;
            if( (red=br.readLine())!=null ){
                ViewModel vm = new ViewModel();
                vm.setBody(red.replace("\\n", "\n"));
                return vm;
            }
            afd.close();
        } catch (Exception e) {
            LogUtil.e("E", "e",e);
        }
        return null;
    }
    
}
